###############################################
# Dynamic DiD: All Visas and Crime Concerns
###############################################

rm(list=ls())

library(Hmisc)
library(readstata13)
library(foreign)
library(tidyverse)
library(did)
library(ggplot2)
set.seed(1111)

#sink("17_results_concerns_all_first.txt")

############################
# Read and prepare data 
############################

# load data
load("final_county_2023april6.RData")
names(d_county)

#######################
# Results
#######################

# Haiti
out2 <- att_gt(yname = "crime_first_problem_s",
               gname = "first.treat_haiti",
               idname = "county",
               tname = "survey",
               data = d_county,
)

es2 <- aggte(out2, type = "dynamic",na.rm = TRUE)
pe2 <- es2$overall.att
se2 <- es2$overall.se
t2 = pe2/se2
pv2 = 2*pnorm(q=t2, lower.tail=F)

# all
out1 <- att_gt(yname = "crime_first_problem_s",
               gname = "first.treat_all",
               idname = "county",
               tname = "survey",
               data = d_county,
)

es1 <- aggte(out1, type = "dynamic",na.rm = TRUE)
pe1 <- es1$overall.att
se1 <- es1$overall.se
t1 = pe1/se1
pv1 = 2*pnorm(q=t1, lower.tail=T)

#######################
# Table
#######################

# Visas from Haiti
pe2 = round(pe2,3)
se2 = round(se2,3) 
pv2 # p-value significant at 0.1

# Visas from All
pe1 = round(pe1,3)
se1 = round(se1,3)
pv1 

columns = c("visas from Haiti","","visas from all countries","","covariates","observations")
concerns = c(pe2,se2,pe1,se1,"no","1,348")
results = cbind(columns,concerns)
results

# Table a11
write.table(results, "tableA11.txt", sep="\t")

sink()
